<?php

namespace App\Models\Elasticsearch;

use Elasticsearch\ClientBuilder;

class Client
{

    /**
     * Elasticsearch 节点主机
     *
     * @var array
     */
    public $hosts;

    /**
     * Client 构造函数
     */
    public function __construct()
    {
        $this->hosts = config('elasticsearch.connections.default.hosts');
    }

    /**
     * Elasticsearch 客户端
     *
     * @var \Elasticsearch\Client
     */
    private $client;

    /**
     * 获取 Elasticsearch 客户端
     *
     * @return \Elasticsearch\Client
     */
    public function client()
    {
        if (is_null($this->client)) {
            $this->client = ClientBuilder::create()->setHosts($this->hosts)->build();
        }
        return $this->client;
    }
}
